<template>
  <div class="inventory-container">
    <el-card class="inventory-card">
      <div slot="header" class="card-header">
        <span>库存状态</span>
        <div class="header-operations">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </div>
      </div>

      <!-- 看板区域 -->
      <div class="dashboard">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-card class="stat-card" shadow="hover">
              <div class="stat-icon">
                <i class="el-icon-goods"></i>
              </div>
              <div class="stat-content">
                <div class="stat-title">现存商品</div>
                <div class="stat-value">{{ dashboardData.totalProductNumber || 0 }}</div>
                <div class="stat-desc">种类</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stat-card" shadow="hover">
              <div class="stat-icon">
                <i class="el-icon-warning"></i>
              </div>
              <div class="stat-content">
                <div class="stat-title">库存预警</div>
                <div class="stat-value warning">{{ dashboardData.alertProduct || 0 }}</div>
                <div class="stat-desc">个商品低于预警值</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stat-card" shadow="hover">
              <div class="stat-icon">
                <i class="el-icon-money"></i>
              </div>
              <div class="stat-content">
                <div class="stat-title">库存总值</div>
                <div class="stat-value">¥{{ formatPrice(dashboardData.totalPrice) }}</div>
                <div class="stat-desc">元</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stat-card" shadow="hover">
              <div class="stat-icon">
                <i class="el-icon-box"></i>
              </div>
              <div class="stat-content">
                <div class="stat-title">库存总量</div>
                <div class="stat-value">{{ dashboardData.totalStock || 0 }}</div>
                <div class="stat-desc">件</div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 表格区域 -->
      <div class="table-container">
        <el-table
          :data="inventoryList"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="productCode" label="商品编号" width="200" align="center"></el-table-column>
          <el-table-column prop="productName" label="商品名称" min-width="200" align="center"></el-table-column>
          <el-table-column prop="categoryName" label="商品分类" width="150" align="center"></el-table-column>
          <el-table-column prop="stock" label="库存数量" width="120" align="center"></el-table-column>
          <el-table-column prop="price" label="单价" width="150" align="center">
            <template slot-scope="scope">
              ¥{{ scope.row.price }}
            </template>
          </el-table-column>
          <el-table-column prop="totalPrice" label="总价" width="150" align="center">
            <template slot-scope="scope">
              ¥{{ scope.row.totalPrice }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="180" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === 'pending' ? 'primary' : 'success'">
                {{ scope.row.status === 'pending' ? '库存预警' : '库存正常' }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页区域 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'InventoryStatus',
  data() {
    return {
      loading: false,
      inventoryList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      queryParams: {
        status: '',
        pageNum: 1,
        pageSize: 10,
        sortField: 'updateTime',
        sortOrder: 'desc'
      },
      statusOptions: [
        { value: 'processed', label: '正常' },
        { value: 'pending', label: '库存预警' }
      ],
      dashboardData: {
        totalProductNumber: 0,
        alertProduct: 0,
        totalPrice: 0,
        totalStock: 0
      }
    }
  },
  created() {
    this.fetchDashboardData();
    this.fetchInventoryList();
  },
  methods: {
    formatPrice(price) {
      if (!price) return '0.00';
      return parseFloat(price).toFixed(2);
    },
    formatDate(date) {
      if (!date) return '';
      return new Date(date).toLocaleString();
    },
    handleSearch() {
      this.currentPage = 1;
      this.fetchDashboardData();
      this.fetchInventoryList();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchInventoryList();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchInventoryList();
    },
    fetchDashboardData() {
      this.$http.get('/inventory/status')
        .then(response => {
          if (response.code === 200) {
            this.dashboardData = response.data;
          }
          // 错误会由全局拦截器处理
        })
        .catch(error => {
          console.error('获取看板数据失败:', error);
          // 错误会由全局拦截器处理
        });
    },
    fetchInventoryList() {
      this.loading = true;
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize,
        status: this.queryParams.status
      };

      this.$http.post('/inventory/list', params)
        .then(response => {
          if (response.code === 200) {
            this.inventoryList = response.data.records || [];
            this.total = response.data.total || 0;
          }
          // 错误会由全局拦截器处理
          this.loading = false;
        })
        .catch(error => {
          console.error('获取库存列表失败:', error);
          // 错误会由全局拦截器处理
          this.loading = false;
        });
    }
  }
}
</script>

<style scoped>
.inventory-container {
  padding: 20px;
  background: #f0f2f5;
  min-height: calc(100vh - 120px);
}

.inventory-card {
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #304156;
}

.header-operations {
  display: flex;
  gap: 10px;
}

.status-select {
  width: 120px;
}

.dashboard {
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 8px;
  display: flex;
  padding: 20px;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  background-color: #ff6b00;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.stat-icon i {
  font-size: 24px;
  color: #fff;
}

.stat-content {
  flex: 1;
}

.stat-title {
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 24px;
  color: #303133;
  font-weight: bold;
  margin-bottom: 4px;
}

.stat-value.warning {
  color: #e6a23c;
}

.stat-desc {
  font-size: 12px;
  color: #909399;
}

.table-container {
  margin-top: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* Element UI 主题覆盖 */
:deep(.el-button--primary) {
  background-color: #ff6b00;
  border-color: #ff6b00;
}

:deep(.el-button--primary:hover),
:deep(.el-button--primary:focus) {
  background-color: #ff8533;
  border-color: #ff8533;
}

:deep(.el-tag--success) {
  background-color: #f0f9eb;
  border-color: #e1f3d8;
  color: #67c23a;
}

:deep(.el-tag--warning) {
  background-color: #fdf6ec;
  border-color: #faecd8;
  color: #e6a23c;
}

:deep(.el-tag--danger) {
  background-color: #fef0f0;
  border-color: #fde2e2;
  color: #f56c6c;
}

:deep(.el-tag--primary) {
  background-color: rgba(156, 39, 176, 0.1);
  border-color: rgba(156, 39, 176, 0.2);
  color: #9C27B0;
}

.el-card {
  border-radius: 8px;
}

.el-card__header {
  padding: 15px 20px;
  border-bottom: 1px solid #e6e6e6;
}

.el-table {
  color: #304156;
}

.el-table th {
  background-color: #f5f7fa;
  color: #304156;
}

:deep(.el-pagination.is-background .el-pager li:not(.disabled).active) {
  background-color: #ff6b00;
}

:deep(.el-pagination.is-background .el-pager li:not(.disabled):hover) {
  color: #ff6b00;
}
</style> 